import { mapState } from 'vuex';
import { TimeFormatter } from '@/libs/utils';
// import destroyEnvTip from './destroyEnvironmentMixin';
export const projectCol = {
    // mixins: [destroyEnvTip],
    data() {
        let _this = this;
        return {
            projectCol: [
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                {
                    type: 'link',
                    title: '名称',
                    key: 'name',
                    minWidth: 200,
                    tooltip: true,
                    on:{
                        click:(row) => {
                            this.goTopo(row)
                        }
                    }
                },
                {
                    title: '部署状态',
                    key: 'statusCHName',
                    minWidth: 110,
                    render: (h, params) => {
                        return h('div', [
                            h(
                                'span',
                                {
                                    style: {
                                        color: _this.$options.filters.deployStatusFilter(
                                            params.row,
                                            'color'
                                        )
                                    }
                                },
                                params.row.statusCHName
                            ),
                            h('WarningTip', {
                                props:{
                                    error:true
                                },
                                style: {
                                    marginLeft: '4px',
                                    display:params.row.errorMessage
                                            ? 'inline-block'
                                            : 'none'
                                }
                            },[
                                h('span',{
                                    slot:"content",
                                    domProps:{
                                        innerHTML:params.row.errorMessage
                                    },
                                })
                            ])
                        ]);
                    }
                },
                {
                    title: '是否为分布式',
                    key: 'type',
                    show:()=> this.$store.state.common.showDistributed,
                    filter: (type) => {
                        return type == 'distributed'
                            ? '是'
                            : '否';
                    },
                    setColor: (type) => {
                        return type == 'distributed' ? 'var(--status-yes)' : 'var(--status-no)';
                    },
                    minWidth: 60
                },
                {
                    title:'虚拟机数量',
                    key:'vmCount',
                    tooltip: true,
                    minWidth: 60
                },
                {
                    title:"创建者",
                    key: 'creatorName',
                    tooltip: true,
                    minWidth: 100
                },
                {
                    title:"创建时间",
                    key: 'created',
                    tooltip: true,
                    minWidth: 135
                },
                {
                    title: '操作',
                    key: 'action',
                    width: 260,
                    opList: [
                        {
                            txt: '修改',
                            disabled: (data) => {
                                return this.setCanModifyClick(data) ? true : false;
                            },
                            on:{
                                click: (data,e) => {
                                    this.editProject(data)
                                }
                            }
                        },
                        {
                            txt: '删除',
                            title:'确定要删除这个工程吗？',
                            type: 'delete',
                            disabled: (data) => {
                                return this.setCanModifyClick(data) ? true : false;
                            },
                            on:{
                                click: (data,e) => {
                                    this.delProject(data)
                                }
                            },
                            tooltip:true
                        },
                        {
                            txt: '清空部署',
                            title:'确定要清空部署吗？',
                            disabled:(item) => {
                                return this.setCanClick(item) ? true : false
                            },
                            on:{
                                click: (data,e) => {
                                    this.clearDpeloyBtn(data)
                                }
                            },
                            tooltip:true
                        },
                        {
                            txt: '分享',
                            on:{
                                click: (data,e) => {
                                    this.shareProject(data)
                                }
                            }
                        },
                        {
                            txt: '可用域',
                            on:{
                                click: (data,e) => {
                                    this.clickZoneBtn(data)
                                } 
                            }
                        }
                    ]
                }
            ]
        };
    },
    filters: {
        deployStatusFilter(item, type) {
            let val = item.status;
            let str = '';
            if (type == 'text') {
                switch (val) {
                    case 0:
                        str = '未部署';
                        break;
                    case 1:
                        str = '部署中';
                        break; //同步成功
                    case 2:
                        str = '待部署';
                        break;
                    case 3:
                        str = '部署成功';
                        break;
                    case 4:
                        str = '删除中';
                        break; //导入失败
                    case 5:
                        str = '已编辑';
                        break;
                    case 6:
                        str = '部署失败';
                        break;
                    case 7:
                        str = '编辑错误';
                        break;
                    case 8:
                        str = '删除失败';
                        break;
                    case 9:
                        str = '部署中 ';
                        break;
                    case 10:
                        str = '未绑定';
                        break;
                    case 11:
                        str = '编辑失败';
                        break;
                    case 12:
                        str = '待删除';
                        break;
                    case 13:
                        str = '清空部署中';
                        break;
                    case 14:
                        str = '清空部署失败';
                        break;
                    case 15:
                        str = '部署校验中';
                        break;
                    case 16:
                        str = '导入中';
                        break;
                    case 17:
                        str = '导入失败';
                        break;
                    case 'init':
                        str = '部署已清空';
                        break;
                    case 'deploying':
                        str = '批次部署中';
                        break;
                    case 'complete':
                        str = '批次部署完成';
                        break;
                    case 'error':
                        str = '批次部署错误';
                        break;
                    case 'queryError':
                        str = '部署查询错误';
                        break;
                    default:
                        break;
                }
            } else {
                switch (val) {
                    case 0:
                    case 2:
                    case 5:
                    case 10:
                    case 'init':
                        str = 'var(--status-pre)';
                        break; //default
                    case 1:
                    case 9:
                    case 15:
                    case 16:
                    case 'deploying':
                        str = 'var(--status-ing)';
                        break; //green
                    case 3:
                    case 'complete':
                        str = 'var(--status-yes)';
                        break;
                    case 6:
                    case 7:
                    case 8:
                    case 11:
                    case 14:
                    case 17:
                    case 'error':
                    case 'queryError':
                        str = 'var(--status-no)';
                        break; //red
                    case 4:
                    case 13:
                        str = 'var(--status-deling)';
                        break;
                    //case 10:str = 'blue';break;
                    default:
                        str = 'var(--status-pre)';
                        break;
                }
            }
            return str;
        },
        // 转义演练、竞赛场景进度的状态码为对应的中文和按钮样式
        progressFilter(val, type) {
            let str = '';
            if (type == 'text') {
                switch (val) {
                    case 0:
                        str = '准备中';
                        break;
                    case 1:
                        str = '准备完毕';
                        break;
                    case 2:
                        str = '进行中';
                        break;
                    case 3:
                        str = '裁判评判阶段';
                        break;
                    case 4:
                        str = '结束';
                        break;
                    case 5:
                        str = '已暂停';
                        break;
                    case 6:
                        str = '删除中';
                        break;
                    case 7:
                        str = '删除失败';
                        break;
                    default:
                        break;
                }
            } else {
                switch (val) {
                    case 0:
                        str = 'var(--status-pre)';
                        break;
                    case 1:
                        str = 'var(--status-pre)';
                        break;
                    case 2:
                        str = 'var(--status-ing)';
                        break;
                    case 3:
                        str = 'var(--status-ing)';
                        break;
                    case 4:
                        str = 'var(--status-deling)';
                        break;
                    case 5:
                        str = 'var(--status-suspend)';
                        break;
                    case 6:
                        str = 'var(--status-deling)';
                        break;
                    case 7:
                        str = 'var(--status-no)';
                        break;
                    default:
                        str = 'var(--status-pre)';
                        break;
                }
            }
            return str;
        }
    },
    computed: {
        ...mapState('compet', ['hasFile']) // 菜单组建中已经获取了是否需要归档的配置，不需要归档时，不展示归档按钮
    },
    methods: {
        // 判断竞赛、演练绑定的工程是否还存在
        available(item) {
            return item.projectId && item.available;
        },
        // 分批部署是否部署失败
        partialDeploymentIsError(item) {
            return ['error', 'init'].includes(item.quickCreateStatus);
        },
        // 计算出结束时间24小时后的销毁时间
        destoryTime(item) {
            return new Date(item.endTime).getTime() + 24 * 60 * 60 * 1000;
        },
        // 调用父组件方法，进入详情页
        toDetailPageTip(item) {
            let extension;
            try {
                extension = JSON.parse(item.extension);
            } catch (e) {
                extension = {};
            }
            const deploymentType = extension.deploymentType;
            if (![2, 4].includes(item.progress)) {
                // 非结束、进行中的状态，需要看工程状态判断能不能进入场景详情页
                if (
                    item.deployStatus != 3 || // 部署成功允许进入，部署清空后处于未部署状态，也允许进入
                    (deploymentType &&
                        ['error', 'deploying', 'queryError'].includes(item.quickCreateStatus))
                ) {
                    //
                    this.$Message.warning('未部署成功不可进入！');
                    return;
                }
            }
            if (item.progress == 2) {
                if (
                    [0, 1, 2, 9].includes(item.deployStatus) ||
                    (deploymentType && item.quickCreateStatus === 'deploying')
                ) {
                    this.$Message.warning('工程部署中不可进入！');
                    return;
                }
            }
            if (!item.startTime || !item.endTime) {
                if (this.setBtnStatus(item, 'modify')) {
                    this.$Message.warning('未设置竞赛起止时间不可进入！');
                }
                return;
            }
            if (item.projectId) {
                this.toDetailPage(item);
            } else {
                this.$Message.warning('工程不存在！');
            }
        },
        // 跳转至拓扑页面
        gotoTopo(scene) {
            if (scene.projectId && scene.deployStatus != 10) {
                // 10：未绑定工程
                window.openTrain(
                    `/TopoProject/index.html#/topologyDisplay/topologys?projectId=${scene.projectId}`
                );
            } else {
                this.$Message.warning('工程不存在！');
            }
        }
    }
};
